package com.shop.cereshop.business.service.billing.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.business.dao.billing.CereShopBillingDAO;
import com.shop.cereshop.business.param.billing.CereShopBillingUpdateParam;
import com.shop.cereshop.business.service.billing.CereShopBillingAttributeService;
import com.shop.cereshop.business.service.billing.CereShopBillingService;
import com.shop.cereshop.commons.domain.billing.CereShopBilling;
import com.shop.cereshop.commons.domain.billing.CereShopBillingAttribute;
import com.shop.cereshop.commons.domain.billing.bo.CereShopBillingBo;
import com.shop.cereshop.commons.domain.billing.vo.CereShopBillingVo;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CereShopBillingServiceImpl extends ServiceImpl<CereShopBillingDAO, CereShopBilling> implements CereShopBillingService {

    @Autowired
    private CereShopBillingAttributeService cereShopBillingAttributeService;
    @Override
    public void addBilling(CereShopBillingBo bo) {
        String time = TimeUtils.yyMMddHHmmss();
        Long skuId = bo.getSkuId();
        Long buyerUserId = bo.getBuyerUserId();
        Integer number = bo.getNumber();
        CereShopBilling cereShopBilling = queryByBuyerUserIdAndSkuId(buyerUserId, skuId);
        if (cereShopBilling == null){
            cereShopBilling = baseMapper.findDataBySkuId(skuId);
            cereShopBilling.setNumber(number);
            cereShopBilling.setCreateTime(time);
            cereShopBilling.setBuyerUserId(buyerUserId);
            save(cereShopBilling);
            List<CereShopBillingAttribute> billingAttributes = cereShopBillingAttributeService.findDataBySkuId(skuId);
            for (CereShopBillingAttribute a : billingAttributes) {
                a.setBillingId(cereShopBilling.getBillingId());
                cereShopBillingAttributeService.save(a);
            }
        }else {
            cereShopBilling.setNumber(cereShopBilling.getNumber() + number);
            cereShopBilling.setUpdateTime(time);
            updateById(cereShopBilling);
        }


    }

    @Override
    public CereShopBilling queryByBuyerUserIdAndSkuId(Long buyerUserId, Long skuId) {
        LambdaQueryWrapper<CereShopBilling> q1 = new LambdaQueryWrapper<>();
        q1.eq(CereShopBilling::getSkuId,skuId);
        q1.eq(CereShopBilling::getBuyerUserId,buyerUserId);
        return getOne(q1);
    }

    @Override
    public List<CereShopBillingVo> queryList(Long buyerUserId, Long shopId) {
        List<CereShopBillingVo> billings = baseMapper.queryList(buyerUserId, shopId);
        for (CereShopBillingVo billing : billings) {
            billing.setValues(EmptyUtils.getImages(billing.getValue()));
        }
        return billings;
    }

    @Override
    public Boolean updateSku(CereShopBillingUpdateParam param) {
        boolean flag = true;
        String time = TimeUtils.yyMMddHHmmss();
        CereShopBilling billing = getById(param.getBillingId());
        CereShopBilling newBilling = baseMapper.findDataBySkuId(param.getNewSkuId());
        billing.setSkuId(newBilling.getSkuId());
        billing.setImage(newBilling.getImage());
        billing.setNumber(param.getNumber());
        billing.setProductPrice(param.getPrice());
        billing.setUpdateTime(time);
        flag = updateById(billing);

        //删除旧数据
        cereShopBillingAttributeService.remove(new LambdaQueryWrapper<CereShopBillingAttribute>().eq(CereShopBillingAttribute::getBillingId,billing.getBillingId()));
        List<CereShopBillingAttribute> billingAttributes = cereShopBillingAttributeService.findDataBySkuId(billing.getSkuId());
        for (CereShopBillingAttribute a : billingAttributes) {
            a.setBillingId(billing.getBillingId());
            flag = cereShopBillingAttributeService.save(a);
        }
        return flag;
    }
}
